package com.touchealth.test.javasissit;

import javassist.*;

import java.io.IOException;

/**
 * @author zhangyanqiang
 * @date 2021/11/18
 **/
public class JavassistUtil {


    public static void addUrl(String className,String url) throws NotFoundException, CannotCompileException, IOException {

        ClassPool classPool = ClassPool.getDefault();
        CtClass  ctClass = classPool.get(className);
        CtMethod ctMethod = ctClass.getDeclaredMethod("isExcludedUri");
        if (ctClass.isFrozen()){
            ctClass.defrost();
        }
        ctMethod.insertBefore("if(uri.indexOf(\""+url+"\")>-1){return true;}");
        ctClass.writeFile("./touchealth-common/target/classes/");
        ctClass.rebuildClassFile();

    }

    public static void removeUrl(String className, String url) throws NotFoundException, CannotCompileException, IOException {

        ClassPool classPool = ClassPool.getDefault();
        CtClass  ctClass = classPool.get(className);
        if (ctClass.isFrozen()){
            ctClass.defrost();
        }
        CtMethod ctMethod = ctClass.getDeclaredMethod("isExcludedUri");
        ctMethod.insertBefore("if(uri.indexOf(\""+url+"\")>-1){return false;}");
        ctClass.writeFile("./touchealth-common/target/classes/");
        ctClass.rebuildClassFile();
    }


}
